package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.acticle.domain.*;
import cn.wolfcode.luowowo.acticle.query.AnswerQuery;
import cn.wolfcode.luowowo.acticle.query.QuerytionsQuery;
import cn.wolfcode.luowowo.acticle.service.IAnswerService;
import cn.wolfcode.luowowo.acticle.service.IDestinationService;
import cn.wolfcode.luowowo.acticle.service.IQueryService;
import cn.wolfcode.luowowo.acticle.vo.AnswerStatisVO;
import cn.wolfcode.luowowo.acticle.vo.PaihangVo;
import cn.wolfcode.luowowo.cache.key.RedisKeys;
import cn.wolfcode.luowowo.cache.service.IAnswerRedisService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.vo.UserCommentVO;
import cn.wolfcode.luowowo.website.web.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.web.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/questionsAndAnswers")
public class WendaController {

    @Reference
    private IQueryService queryService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IAnswerService answerService;

    @Reference
    private IAnswerRedisService answerRedisService;

    @RequireLogin
    @RequestMapping("public")
    public String input(Model model){
        List<Destination> dest = destinationService.getDestByDeep(3);
        model.addAttribute("dests",dest);
        return "questionsAndAnswers/public";
    }

    @RequestMapping("wenda")
    public String wenda(Model model, @ModelAttribute("qo")QuerytionsQuery qo){
        PageInfo pageInfo =  queryService.queryForList(qo);
        List<Query> queryList = pageInfo.getList();
        for (Query query : queryList) {
            Answer answer = answerService.getAnswerByThumbupnum(query.getId());
            query.setAnswer(answer);
        }
        model.addAttribute("pageInfo",pageInfo);

        if(qo.getPaihangType()==2){
            List<AnswerStatisVO> volist = answerRedisService.getAllVo(RedisKeys.ANSWER_STATIS_THUMB_USER.getPrefix(),0,9);
            List<PaihangVo> paihangvo = new ArrayList<>();
            for (AnswerStatisVO vo : volist) {
                PaihangVo phvo = new PaihangVo();
                phvo.setUserName(vo.getUserName());
                phvo.setHeadImgUrl(vo.getHeadImgUrl());
                phvo.setLevel(vo.getLevel());
                phvo.setNum(vo.getThumbupnum());
                paihangvo.add(phvo);
            }
            model.addAttribute("paihangvo",paihangvo);
        }
        if(qo.getPaihangType()==1){
            List<UserCommentVO> volist = answerRedisService.getAllUVo(RedisKeys.USER_STATIS_VO_COMMENT_USER.getPrefix(),0,9);
            List<PaihangVo> paihangvo = new ArrayList<>();
            for (UserCommentVO vo : volist) {
                PaihangVo phvo = new PaihangVo();
                phvo.setUserName(vo.getUserName());
                phvo.setHeadImgUrl(vo.getHeadImgUrl());
                phvo.setLevel(vo.getLevel());
                phvo.setNum(vo.getCommentnum());
                paihangvo.add(phvo);
            }
            model.addAttribute("paihangvo",paihangvo);
        }
        return "questionsAndAnswers/wenda";
    }

    @RequestMapping("wenda2")
    public String wenda2(Model model, @ModelAttribute("qo")QuerytionsQuery qo){
        PageInfo pageInfo =  queryService.queryForList(qo);
        List<Query> queryList = pageInfo.getList();
        for (Query query : queryList) {
            Answer answer = answerService.getAnswerByThumbupnum(query.getId());
            query.setAnswer(answer);
        }
        model.addAttribute("pageInfo",pageInfo);

        if(qo.getPaihangType()==2){
            List<AnswerStatisVO> volist = answerRedisService.getAllVo(RedisKeys.ANSWER_STATIS_THUMB_USER.getPrefix(),0,9);
            List<PaihangVo> paihangvo = new ArrayList<>();
            for (AnswerStatisVO vo : volist) {
                PaihangVo phvo = new PaihangVo();
                phvo.setUserName(vo.getUserName());
                phvo.setHeadImgUrl(vo.getHeadImgUrl());
                phvo.setLevel(vo.getLevel());
                phvo.setNum(vo.getThumbupnum());
                paihangvo.add(phvo);
            }
            model.addAttribute("paihangvo",paihangvo);
        }
        if(qo.getPaihangType()==1){
            List<UserCommentVO> volist = answerRedisService.getAllUVo(RedisKeys.USER_STATIS_VO_COMMENT_USER.getPrefix(),0,9);
            List<PaihangVo> paihangvo = new ArrayList<>();
            for (UserCommentVO vo : volist) {
                PaihangVo phvo = new PaihangVo();
                phvo.setUserName(vo.getUserName());
                phvo.setHeadImgUrl(vo.getHeadImgUrl());
                phvo.setLevel(vo.getLevel());
                phvo.setNum(vo.getCommentnum());
                paihangvo.add(phvo);
            }
            model.addAttribute("paihangvo",paihangvo);
        }
        return "questionsAndAnswers/topTpl";
    }

    @RequireLogin
    @RequestMapping("wendaDetail")
    public String wendaDetail(Long id,Model model,@UserParam UserInfo userInfo, @ModelAttribute("qo")AnswerQuery qo){
        //问题回显
        Query query = queryService.getQueryByid(id);
        QueryContent queryContent = queryService.getContent(id);

        //回答返显
        PageInfo pageInfo = answerService.getAnswerByqo(qo);
        List<Answer> list = pageInfo.getList();
        for (Answer answer : list) {
            AnswerContent content = answerService.getContentByAnswer(answer.getId());
            answer.setAnswerContent(content);
            AnswerStatisVO vo = answerRedisService.getStatisvo(answer.getId());
            answer.setThumbupnum(vo.getThumbupnum());
        }
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("query",query);
        model.addAttribute("queryContent",queryContent);
        model.addAttribute("userInfo",userInfo);
        return "questionsAndAnswers/wendaDetail";
    }

    @RequestMapping("querySubmission")
    @ResponseBody
    public AjaxResult querySubmission(Model model, Query query,@UserParam UserInfo userInfo){
        query.setAuthor(userInfo);
        queryService.saveQuery(query);
        return AjaxResult.SUCCESS();
    }
}
